package com.nega.modules.thread;

import com.nega.commons.model.SearchResult;
import com.nega.commons.spider.IPSourcePool;
import com.nega.commons.spider.SpiderCoreService;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @Author: Garcia
 * @CreateDate: 2019/5/13 18:36
 * @Description: what it is class?
 */
@Slf4j
public class SpiderThreadSouGou implements Callable<List<SearchResult>> {

    private              SpiderCoreService   spiderCoreService;
    private              IPSourcePool ipSourcePool;
    private              int                 num;
    private              String              text;
    private static final String              DOMAIN  = "https://www.sogou.com";
    private static final String              CONTEXT = "/web";

    public SpiderThreadSouGou() {

    }

    public SpiderThreadSouGou(SpiderCoreService spiderCoreService, IPSourcePool ipSourcePool, String text, int num) {
        this.num = num;
        this.spiderCoreService = spiderCoreService;
        this.text = text;
        this.ipSourcePool = ipSourcePool;
    }

    @Override
    public List<SearchResult> call() {
        List<SearchResult> results = new ArrayList<>();
        Map<String, String> params = getParams(text);
        int size = (num * 10);
        String ip = ipSourcePool.getIp();
        for (int i = (size - 9); i < size; i++) {
            params.put("pn", String.valueOf(i));
            Document document = spiderCoreService.getDocument(DOMAIN + CONTEXT, params, ip);
            results.addAll(getInfo(document, i));
            if (isLastPage(document)) {
                break;
            }
        }
        return results;
    }

    private Map<String, String> getParams(String text) {
        Map<String, String> params = new HashMap<>();
        params.put("query", text);
        params.put("from", "index-nologin");
        params.put("sugsuv", "00C801EB76CD3FC25B4AACC57D555101");
        params.put("sut", "3553");
        params.put("sugtime", "1557298528981");
        params.put("lkt", "1%2C1557298526719%2C1557298526719");
        params.put("s_from", "index");
        params.put("sst0", "1557298528981");
        params.put("ie", "utf-8");
        params.put("p", "40040100");
        params.put("dp", "1");
        params.put("w", "01019900");
        params.put("dr", "1");
        return params;
    }


    private boolean isLastPage(Document document) {
        if (document == null) {
            return true;
        }
        try {
            Element element = document.getElementById("pagebar_container");
            if (element == null){
                return true;
            }
            Elements elements = element.getElementsByTag("a");
            for (Element ele : elements) {
                if ("下一页".equals(ele.text())) {
                    return false;
                }
            }
        } catch (Exception e) {
            log.error("搜狗检测下一页异常", e);
        }
        return true;
    }

    private List<SearchResult> getInfo(Document document, int page) {
        List<SearchResult> results = new ArrayList<>();
        if (document == null) {
            return results;
        }
        Element main = document.getElementById("main");
        if (main == null){
            return results;
        }
        Element resul = main.getElementsByAttributeValue("class", "results").get(0);
        Elements vrwraps = resul.getElementsByClass("vrwrap");
        for (int i = 0; i < vrwraps.size(); i++) {
            Element ele = vrwraps.get(i);
            try {
                Elements strBox = ele.getElementsByAttributeValue("class", "strBox");
                if (strBox.size() < 1) {
                    continue;
                }
                SearchResult result = new SearchResult();
                result.setKeyWord(text);
                Elements wx_titles = ele.getElementsByAttributeValueContaining("id","wx_title");
                Element vrTitle = ele.getElementsByAttributeValue("class", "vrTitle").get(0);
                Element aTitle_url = vrTitle.getElementsByTag("a").get(0);
                if (wx_titles.size()>0){
                    Element wx_title = wx_titles.get(0);
                    result.setTitle(wx_title.text());
                    result.setUrl(DOMAIN + aTitle_url.attr("href"));
                    Elements wx_datas = ele.getElementsByAttributeValueContaining("id","wx_data");
                    if (wx_datas.size()>0){
                        result.setContent(wx_datas.get(0).text());
                    }
                    Element cite = ele.getElementsByClass("fb").get(0);
                    String sourceandtime = cite.text();
                    String sps [] = sourceandtime.split(" - ");
                    result.setTime(sps[sps.length-1]);
                    result.setSource(sps[0]);
                }else {
                    result.setTitle(aTitle_url.text());
                    result.setUrl(DOMAIN + aTitle_url.attr("href"));
                    Elements info = ele.getElementsByAttributeValue("class", "str_info");
                    if (info.size() > 0) {
                        result.setContent(info.get(0).text());
                    }
                    Element cite = ele.getElementsByAttributeValueContaining("id", "cacheresult_info").first();
                    if (cite != null){
                        result.setSource(cite.text());
                        Elements dates = cite.getElementsByTag("date");
                        if (dates.size() > 0) {
                            result.setTime(dates.get(0).text());
                        }
                    }
                }
                results.add(result);
            } catch (Exception e) {
                log.error("搜狗抓取第[{}]页，显示排位第[{}]位异常", page, i, e);
            }
        }
        return results;
    }

}
